Fix param of Type::from_xdr_base64 #180
Merged
+17
−17
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What
Change Type::from_xdr_base64 fn to accept any type that as-refs to [u8] instead of accepting a String.
Why
There's three reasons.
The first is that by accepting a
String
we're requiring the caller to provide an owned value to the function. The function doesn't need an owned value, so the request for an owned value is unnecessary.The second is that by accepting a
String
we're requiring that the caller to provide a value that is stored in memory allocated by the global allocator (the heap). Again this is unnecessary.The third is that the base64 may not be stored in a string, it might have been communicated over a network or some other medium where we've got it in bytes, and converting it to a string before passing it to the decoder is unnecessary work that has fallible cases to handle because Rust strs are required to be UTF-8.
There is no cost introduced with this change. The same approach on other types is generated in this way and it appears to be a mistake we didn't do it the same way here.